package com.example.studykotlin

import android.os.Bundle
import android.os.Looper
import android.widget.Toast
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.studykotlin.databinding.ActivityMainBinding
import com.example.studykotlin.kotlin_actual_combat.BaseActivity.BaseActivity
import com.example.studykotlin.kotlin_actual_combat.Bean.ItemSingleClickMessage
import com.example.studykotlin.kotlin_actual_combat.annotation.EventMode
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainActivity : BaseActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)

        initView()
        initData()

    }

    /**
     * 初始化应用主界面列表
     */
    private fun initView() {
        binding.recycler.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        binding.recycler.addItemDecoration(
            DividerItemDecoration(
                this,
                DividerItemDecoration.VERTICAL
            )
        )

    }

    private fun initData() {
        val data = mutableListOf(
            resources.getStringArray(R.array.studyName)[0],
            resources.getStringArray(R.array.studyName)[1],
            resources.getStringArray(R.array.studyName)[2],
            resources.getStringArray(R.array.studyName)[3],
            resources.getStringArray(R.array.studyName)[4],
            resources.getStringArray(R.array.studyName)[5],
            resources.getStringArray(R.array.studyName)[6],
        )
        listAdapter = ListAdapter(data)
        listAdapter.eventMode = EventMode.MAIN
        binding.recycler.adapter = listAdapter
    }


    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun handleItemSingleClickMessage(message: ItemSingleClickMessage) {
        if (message.eventMode != EventMode.MAIN) return
        when (message.position) {
            0 -> {
                Toast.makeText(this, "Kotlin实战", Toast.LENGTH_SHORT).show()
                ARouter.getInstance().build(Path.KotlinActualCombat).navigation()
            }
            1 -> {
                Toast.makeText(this, "Kotlin官方参考文档 中文版", Toast.LENGTH_SHORT).show()
                ARouter.getInstance().build(Path.KotlinDocument).navigation()
            }
            2 -> {
                Toast.makeText(this, "kotlin-for-android-developers", Toast.LENGTH_SHORT)
                    .show()
            }
            3 -> {
                Toast.makeText(this, "kotlin从入门到进阶实战", Toast.LENGTH_SHORT).show()
            }
            4 -> {
                Toast.makeText(this, "我的kotlin", Toast.LENGTH_SHORT).show()
                ARouter.getInstance().build(Path.myKotlin).navigation()
            }
            5 -> {
                Toast.makeText(this, "Android 开发艺术探索", Toast.LENGTH_SHORT).show()
                ARouter.getInstance().build(Path.art).navigation()
            }
            6 -> {
                Toast.makeText(this, "Android 进阶之光", Toast.LENGTH_SHORT).show()
                ARouter.getInstance().build(Path.advanced).navigation()
            }
        }
    }

}